<template>
 	<view class="Page">
		<view class="title">设备树节点</view>
		<view class="tree">
			<button @click="equipmentSelect" class="treeBtn" type="primary">设备树</button>
			<image src="../../static/scan_icon.svg" mode="aspectFit" @click="scan_tree"></image>
		</view>
		<view class="noData" v-show="!NodeItem">当前无设备树节点</view>
		<view class="data_item" v-if="NodeItem">
			<view class="card-left">
				<view>{{NodeItem?.display_name}}</view>
			</view>
		</view>
		<view class="title">停机事件</view>
		<view class="noData" v-show="!stopList">当前无停机时间</view>
		
		<view v-if="stopList" class="select">
			 <uni-data-select
				:localdata="stopList"
				@change="change"
			  ></uni-data-select>
		</view>

		<uni-popup ref="is_xiaxian" type="dialog">
			<uni-popup-dialog type="success" cancelText="取消" confirmText="确定" title="通知" content="是否确定下线" @confirm="xiaxianConfirm"
				></uni-popup-dialog>
		</uni-popup>
 		<view class="kongbai">
 			
 		</view>
 	</view>

	<view class="buttonBox">
		<button size="default" type="primary"  @click="xiaxian">下线</button>
	</view>
 </template>
 
 <script>
 	export default {
 		data() {
 			return {
 				list:[],
 				org_id:34,
				treeList:[],
				NodeItem:'',
				stopList:'',
				stopId:'',
				year:'',
 			}
 		},
 		onLoad(option) {
			if(option.item){
				this.NodeItem=JSON.parse(option.item)
				
				let that=this;
				that.$root.http('http://113.15.231.21:180/roll/ding_app/query_stop_event_list',
					{
						org_id:21,               // 组织ID 写 21
						aim_org_id: that.NodeItem.used_org_id,           // 停机事件组织id，这里从设备树节点obj数据中的used_org_id获取
						page_size: 100,            // 每页100条数据
						page_index: 1,             // 父级id，第一层级不传
					}, 
					{
						sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							console.log("stop")
							console.log(res)
							if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
								that.stopList = res.data.result.list.map(obj => {
								  let startYear = obj?.error_start_time.slice(0, 4);
								  let endYear = obj?.error_end_time.slice(0, 4);
								
								  let startTime, endTime;
								  if (startYear == that.year) {
									startTime = obj?.error_start_time.slice(5, 16)
								  } else {
									startTime = obj?.error_start_time.slice(0, 16)
								  }
								  if (endYear == that.year) {
									endTime = obj?.error_end_time.slice(5, 16)
								  } else {
									endTime = obj?.error_end_time.slice(0, 16)
								  }
								
								  return {
									text: startTime + ' - ' + endTime,
									value: obj?.id,
									disable: false
								  }
								})	
								that.stopList=that.stopList.reverse()
							}
							else{
								that.$root.message('fail','停机事件无数据')
							}
						}
						that.$root.judge(res,fun);
					})
			}
		
 		},
 		methods: {
			scan_tree(){
				let that=this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function (res) {
						console.log(res.result)
						that.if_scan=true;
						let strArray=res.result.split(',')
						console.log(strArray)
						if(strArray[0][5]!='e'){
							that.$root.message('fail','扫的码不是设备树节点码')
						}
						else{
							// that.treeID=strArray[0].slice(12);
							
							that.$root.http('http://113.15.231.21:180/roll_device/ding_app/query_device_tree_list',
								{
									  org_id: 21,               // 组织ID 写 21
									  is_active: true,           // 默认传
									  page_index: 1,             // 第一页
									  ids:strArray[0].slice(12),
								}, 
								{
									sessionid:that.$root.sessionID
								})
								.then((res) => {
									let fun=function(){
										// that.list=[];
										if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
											that.NodeItem=res.data.result.list[0];
											
											that.$root.http('http://113.15.231.21:180/roll/ding_app/query_stop_event_list',
												{
													org_id:21,               // 组织ID 写 21
													aim_org_id: that.NodeItem.used_org_id,           // 停机事件组织id，这里从设备树节点obj数据中的used_org_id获取
													pageSize: 100,            // 每页100条数据
													page_index: 1,             // 父级id，第一层级不传
												}, 
												{
													sessionid:that.$root.sessionID
												})
												.then((res) => {
													let fun=function(){
														console.log("stop")
														console.log(res)
														if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
															that.stopList=res.data.result.list.map(obj=>
																({
																	text:obj?.error_start_time+'-'+obj?.error_end_time,
																	value:obj?.id,
																	disable:false
																})
															)
															// that.$root.message('fail',that.stopList)
															that.stopList=that.stopList.reverse()
															console.log(that.stopList)
													 
														}
														else{
															that.$root.message('fail','停机事件无数据')
														}
													}
													that.$root.judge(res,fun);
												})
										}
										else{
											that.$root.message('fail','设备树无数据')
										}
										
									}
									that.$root.judge(res,fun);
								})
						}
				 
					},
				});
			},
			equipmentSelect(){
				let that=this;
				that.$root.http('http://113.15.231.21:180/roll_device/ding_app/query_device_tree_list',
					{
						  org_id: 21,               // 组织ID 写 21
						  is_active: true,           // 默认传
						  page_index: 1,             // 第一页
						  pageSize: 7,              // 每页8条数据
						  // parent_id: 12,             // 父级id，第一层级不传
					}, 
					{
						sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							console.log(res)
							if(res.data.result&&res.data.result.list&&res.data.result.list.length>0){
								console.log(res.data.result.list);
								that.treeList=res.data.result.list
								console.log(that.treeList);
								dd.navigateTo({
									url:'/pages/shangxian/locationNode?list='+ JSON.stringify(that.treeList)+'&isShangxian=0',
									
								})
						 
							}
							else{
								// this.$refs.idbucunzai.open('center')
								that.$root.message('fail','设备树无数据')
							}
						}
						that.$root.judge(res,fun);
					})
			},
			xiaxian(){
				this.$refs.is_xiaxian.open('center')
			},
			change(e) {
			    this.stopId=e;
			},
			xiaxianConfirm(){
				let that=this;
				that.$root.http('http://113.15.231.21:180/roll_device/ding_app/device_record_down',
					{
						org_id: 21,                          // 组织ID 写死 21
						stop_event_down_id: that.stopId,                // 停机时间id
						device_tree_id: that.NodeItem?.id,                  // 设备树节点id    
					}, 
					{
						sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							
						}
						that.$root.judge(res,fun);
					})
			}
			
 		}
 	}
 </script>
 
 <style scoped>
 	.Page{
 		width: 100%;
 		font-size: 14px;
  		background-color: #F6F6F6;
 		display: flex;
 		flex-direction: column;
 		/* align-items: center; */
		padding-left: 10px;
		padding-right: 10px;
 		
 	}
	.tree{
		width: 100%;
		height: 40px;
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 12px;
	}
	.tree image{
		width: 40px;
		height: 40px;
		margin-left: 15px;
	}
	.treeBtn{
		width: 20%;
		height: 40px;
		font-size: 14px;
		line-height: 40px;
	}
 	.title{
 		font-size: 16px;
 		margin-top: 15px;
 		font-weight: 600;
		border-left: 2px solid dodgerblue;
		padding-left: 8px ;
 	}
 	.noData{
 		margin-top: 15px;
 	}
 	.select{
		margin-top: 12px;
		width: 93%;
	}
 
 	.data_item{
 		margin-top: 12px;
 		width: 90%;
 		background-color: white;
 		border-radius: 5px;
 		font-size: 14px;
 		padding-left: 10px;
 		padding-bottom: 5px;
 		display: flex;
 		flex-direction: row;
 		justify-content: space-between;
 		
 	}
 	.card-left view{
 		margin-top: 5px;
 	}
	.input input{
		width: 100%;
		color: gray;
	}
 	.gray{
 		color: #93A1A1;
 	}
 	.num-icon{
 		width: 16px;
 		height: 16px;
 	}
 	.is_show_box{
 		width: 100%;
 	}
 	.kongbai{
 		width: 100%;
 		height: 60px;
 	}
	.buttonBox{
		width: 100%;
		position: fixed;
		bottom: 0%;
	}
	.buttonBox button{
		width: 100%;
	}
 	.scanButton{
 		background-color: #42B983;
 		color: white;
 		border: 1px solid #42B983;
 	}
 </style>
 